package com.change.zh.ffmpegdo.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

@RestController
public class RtmpManager {


    /**
     * 推流验证
     * nginx启动nginx.exe -c conf\nginx-win-rtmp.conf
     *
     * @param request  app=live
     *                 flashver=
     *                 swfurl=
     *                 tcurl=rtmp://localhost:1935/live
     *                 pageurl=
     *                 addr=127.0.0.1
     *                 clientid=15
     *                 call=publish
     *                 name=bamboo
     *                 type=live
     *                 pass=123456
     * @param response
     * @return
     */
    @GetMapping(value = "onpublish")
    public String onPublish(HttpServletRequest request, HttpServletResponse response) {
        final Map<String, Object> map = new HashMap<>();
        Enumeration parameterNames = request.getParameterNames();
        while (parameterNames.hasMoreElements()) {
            String key = (String) parameterNames.nextElement();
            map.put(key, request.getParameter(key));
        }
        for (Map.Entry obj : map.entrySet()) {
            System.out.println("onpublish***" + obj.getKey() + ":" + obj.getValue()+"=>"+(obj.getValue().equals("")));
        }
        try {
            if (map.get("pass").equals("123456")) {
                response.setStatus(200);
                return "{\"code\":\"200\",\"detail\":\"SUCCESS\"}";
            } else {
                response.setStatus(500);
                return "{\"code\":\"500\",\"detail\":\"auth error\"}";
            }
        } catch (Exception e) {
            response.setStatus(500);
            return "{\"code\":\"500\",\"detail\":\"sys error\"}";
        }
    }

    /**
     *
     * @param request
     * onpublishdone***app:live=>false
     * onpublishdone***call:publish_done=>false
     * onpublishdone***tcurl:rtmp://localhost:1935/live=>false
     * onpublishdone***clientid:1=>false
     * onpublishdone***pass:123456=>false
     * onpublishdone***flashver:=>true
     * onpublishdone***swfurl:=>true
     * onpublishdone***name:bamboo=>false
     * onpublishdone***pageurl:=>true
     * onpublishdone***addr:127.0.0.1=>false
     * @param response
     * @return
     */
    @GetMapping(value = "onpublishdone")
    public String onPublishDone(HttpServletRequest request, HttpServletResponse response) {
        final Map<String, Object> map = new HashMap<>();
        Enumeration parameterNames = request.getParameterNames();
        while (parameterNames.hasMoreElements()) {
            String key = (String) parameterNames.nextElement();
            map.put(key, request.getParameter(key));
        }
        for (Map.Entry obj : map.entrySet()) {
            System.out.println("onpublishdone***" + obj.getKey() + ":" + obj.getValue()+"=>"+(obj.getValue().equals("")));
        }
        response.setStatus(200);
        return "{\"code\":\"200\",\"detail\":\"SUCCESS\"}";
    }


    /**
     *
     * @param request
     * onplay***app:live=>false
     * onplay***call:play=>false
     * onplay***duration:0=>false
     * onplay***tcurl:rtmp://localhost:1935/live=>false
     * onplay***clientid:6=>false
     * onplay***flashver:LNX 9,0,124,2=>false
     * onplay***swfurl:=>true
     * onplay***name:bamboo=>false
     * onplay***start:4294965296=>false
     * onplay***reset:0=>false
     * onplay***pageurl:=>true
     * onplay***addr:127.0.0.1=>false
     * @param response
     * @return
     */
    @GetMapping(value = "onplay")
    public String onPlay(HttpServletRequest request, HttpServletResponse response) {
        final Map<String, Object> map = new HashMap<>();
        Enumeration parameterNames = request.getParameterNames();
        while (parameterNames.hasMoreElements()) {
            String key = (String) parameterNames.nextElement();
            map.put(key, request.getParameter(key));
        }
        for (Map.Entry obj : map.entrySet()) {
            System.out.println("onplay***" + obj.getKey() + ":" + obj.getValue()+"=>"+(obj.getValue().equals("")));
        }
        response.setStatus(200);
        return "{\"code\":\"200\",\"detail\":\"SUCCESS\"}";
    }

    /**
     *
     * @param request
     * onplaydone***app:live=>false
     * onplaydone***call:play_done=>false
     * onplaydone***tcurl:rtmp://localhost:1935/live=>false
     * onplaydone***clientid:6=>false
     * onplaydone***flashver:LNX 9,0,124,2=>false
     * onplaydone***swfurl:=>true
     * onplaydone***name:bamboo=>false
     * onplaydone***pageurl:=>true
     * onplaydone***addr:127.0.0.1=>false
     * @param response
     * @return
     */
    @GetMapping(value = "onplaydone")
    public String onPlayDone(HttpServletRequest request, HttpServletResponse response) {
        final Map<String, Object> map = new HashMap<>();
        Enumeration parameterNames = request.getParameterNames();
        while (parameterNames.hasMoreElements()) {
            String key = (String) parameterNames.nextElement();
            map.put(key, request.getParameter(key));
        }
        for (Map.Entry obj : map.entrySet()) {
            System.out.println("onplaydone***" + obj.getKey() + ":" + obj.getValue()+"=>"+(obj.getValue().equals("")));
        }
        response.setStatus(200);
        return "{\"code\":\"200\",\"detail\":\"SUCCESS\"}";
    }

    @GetMapping(value = "hlsonpublish")
    public String hlsOnPublish(HttpServletRequest request, HttpServletResponse response) {
        final Map<String, Object> map = new HashMap<>();
        Enumeration parameterNames = request.getParameterNames();
        while (parameterNames.hasMoreElements()) {
            String key = (String) parameterNames.nextElement();
            map.put(key, request.getParameter(key));
        }
        for (Map.Entry obj : map.entrySet()) {
            System.out.println("hlsonpublish***" + obj.getKey() + ":" + obj.getValue()+"=>"+(obj.getValue().equals("")));
        }
        response.setStatus(200);
        return "{\"code\":\"200\",\"detail\":\"SUCCESS\"}";
    }

    @GetMapping(value = "hlsondone")
    public String hlsOnDone(HttpServletRequest request, HttpServletResponse response) {
        final Map<String, Object> map = new HashMap<>();
        Enumeration parameterNames = request.getParameterNames();
        while (parameterNames.hasMoreElements()) {
            String key = (String) parameterNames.nextElement();
            map.put(key, request.getParameter(key));
        }
        for (Map.Entry obj : map.entrySet()) {
            System.out.println("hlsondone***" + obj.getKey() + ":" + obj.getValue()+"=>"+(obj.getValue().equals("")));
        }
        response.setStatus(200);
        return "{\"code\":\"200\",\"detail\":\"SUCCESS\"}";
    }
}
